-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add from_bytes_dict alternative constructor for HttpResponseHeaders #33
Conversation
Codecov Report
@@ Coverage Diff @@
## master #33 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 5 6 +1
Lines 127 224 +97
=========================================
+ Hits 127 224 +97
|
alongside a plain ``bytes`` value. | ||
|
||
By default, it converts the ``bytes`` value using "utf-8". However, this | ||
can easily be overridden using the ``encoding`` parameter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This utf-8
default value was based on how Scrapy was using it as the default for its Headers.
web_poet/page_inputs.py
Outdated
@@ -74,6 +75,45 @@ def from_name_value_pairs(cls: Type[T_headers], arg: List[Dict]) -> T_headers: | |||
""" | |||
return cls([(pair["name"], pair["value"]) for pair in arg]) | |||
|
|||
@classmethod | |||
def from_bytes( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name may be a bit misleading, one may think this creates Headers from HTTP response bytes which represent the headers. Maybe something like from_bytes_dict
? Or maybe even more specific, from_scrapy_headers
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! Renaming it to from_scrapy_headers
seems to be too specific. Other non-scrapy libs could have the same setup. Having it as from_bytes_dict
makes it more clear and semantically more meaningful at the same time. Updated this on eb18427
if isinstance(data, str): | ||
return data | ||
elif isinstance(data, bytes): | ||
return data.decode(encoding) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should raise an exception if data is not bytes or str
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Adressed this in ddb7d20 alongside the ability to handle Tuple
alongside List
.
Thanks @BurnzZ! |
From the recent enhancement in #30, it would seem that we'll need to support insantiating the
HttpResponseHeaders
from raw bytes values. In particular, frameworks like Scrapy uses header values in bytes format.By default, https://github.com/aio-libs/multidict only supports strings and not bytes. Thus, an alternative constructor is proposed for ease of use.